﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TY.Utilities
{
    /// <summary>
    /// Log4Net日志类
    /// </summary>
    public class LogHelper
    {
        private ILog logger;

        public LogHelper(ILog log)
        {
            this.logger = log;
        }
        public void Debug(object message)
        {
            this.logger.Debug(message);
        }
        public void Debug(object message, Exception e)
        {
            this.logger.Debug(message, e);
        }
        public void Error(object message)
        {
            this.logger.Error(message);
        }
        public void Error(object message, Exception e)
        {
            this.logger.Error(message, e);
        }
        public void Info(object message)
        {
            this.logger.Info(message);
        }
        public void Warn(object message)
        {
            this.logger.Warn(message);
        }
        /// <summary>
        /// 写入日志（log4net）
        /// </summary>
        /// <param name="ex">异常对象</param>
        public static void WriteLog(Exception ex)
        {
            WriteLog(ex.Message, ex);
        }
        /// <summary>
        /// 写入日志（log4net）
        /// </summary>
        /// <param name="ex">异常对象</param>
        public static void WriteLog(string displayStr, Exception ex)
        {
            var log = LogFactory.GetLogger(ex.Message);
            Exception Error = ex;
            LogMessage logMessage = new LogMessage();
            logMessage.OperationTime = DateTime.Now;
            logMessage.Class = ex.Source;
            logMessage.UserName = "";
            if (Error.InnerException == null)
            {
                logMessage.ExceptionInfo = Error.Message;
            }
            else
            {
                logMessage.ExceptionInfo = Error.Message;
            }
            logMessage.ExceptionInfo = Error.Message;
            logMessage.ExceptionSource = Error.Source;
            logMessage.ExceptionRemark = Error.StackTrace;
            string strMessage = new LogFormat().ExceptionFormat(logMessage);
            log.Error(strMessage);

        }
    }
}
